<?php
namespace ITSociaal\Themes\HuisVanHerstel\Hooks;

use ITSociaal\Themes\HuisVanHerstel\MenuFacade;
use ITSociaal\Themes\HuisVanHerstel\Site;
use ITSociaal\Themes\HuisVanHerstel\Traits\CreateObject;
use ITSociaal\Themes\HuisVanHerstel\Traits\ToProtocolRelative;
use Timber as Lumber;
use TimberMenu;
use Twig_Extension_Debug;
use Twig_Extension_StringLoader;
use Twig_SimpleFilter;

/**
 * Timber-specific hooks
 */
class Timber implements HooksInterface
{
	use CreateObject;
	use ToProtocolRelative;

	/** @var \ITSociaal\Themes\HuisVanHerstel\Site */
	protected $site;

	/**
	 * Get or create Site instance
	 */
	protected function getSite()
	{
		return $this->site = $this->site ?: $this->createObject(Site::class);
	}

	/**
	 * Check if Timber library plugin is enabled
	 *
	 * @return bool
	 */
	protected function isTimberActivated()
	{
		return class_exists(Lumber::class, false);
	}

	/**
	 * Adjusts hooks before they're installed
	 */
	public function adjustHooks($actions, $filters)
	{
		if ($this->isTimberActivated()) {
			unset($actions->admin_notices);
			unset($filters->template_include);
		}

		$filters->get_twig->tag = "timber/twig";
	}

	/**
	 * Things to do before any hooks are triggered
	 */
	public function setup()
	{
		if (!$this->isTimberActivated()) return;

		Lumber::$dirname = ["templates", "views"];
	}

	/**
	 * timber_context filter handler
	 *
	 * @param array $context
	 *
	 * @return array
	 */
	public function filterTimberContext($context)
	{
		$context["menu"]  = $this->createObject(MenuFacade::class);
		$context["site"]  = $this->getSite();
		$context["theme"] = $context["site"]->theme;

		return $context;
	}

	/**
	 * get_twig filter handler
	 *
	 * @param \Twig_Environment $twig
	 *
	 * @return \Twig_Environment
	 */
	public function filterGetTwig($twig)
	{
		$indent = $this->createObject(
			Twig_SimpleFilter::class,
			"indent",
			[$this, "twigIndent"]
		);

		$twig->addFilter($indent);

		$twig->addExtension(
			$this->createObject(Twig_Extension_StringLoader::class)
		);

		if (defined("WP_DEBUG") && constant("WP_DEBUG")) {
			$twig->enableDebug();
			$twig->enableStrictVariables();

			$twig->addExtension(
				$this->createObject(Twig_Extension_Debug::class)
			);
		}

		return $twig;
	}

	/**
	 * template_include filter handler
	 */
	public function filterTemplateInclude()
	{
		return get_template_directory() . "/static/no-timber.html";
	}

	/**
	 * timber_image_src filter handler
	 *
	 * @param string $src
	 *
	 * @return string
	 */
	public function filterTimberImageSrc($src)
	{
		return $this->toProtocolRelative($src);
	}

	/**
	 * admin_notices action handler
	 */
	public function onAdminNotices()
	{
		include get_template_directory() . "/templates/admin-timber-error.php";
	}

	/**
	 * indent twig filter callback
	 *
	 * @param string $string
	 * @param int    $tabs
	 *
	 * @return string
	 */
	public function twigIndent($string, $tabs=0)
	{
		$indent = str_repeat("\t", $tabs);

		return preg_replace("/^/m", $indent, $string);
	}
}

/* vi:set ts=4 sw=4 noet: */
